//
//  XKUpdateLoginPwdView.m
//  XKJY
//
//  Created by xiaokang100 on 2017/1/17.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKUpdateLoginPwdView.h"


@interface XKUpdateLoginPwdView () <UITextFieldDelegate>

// 白背景
@property (nonatomic, strong) UIView *bgView;

// 提示标题
@property (nonatomic, strong) UILabel *remindLabel;         // 安全提示
@property (nonatomic, strong) UILabel *pwdTitleLabelZero; // 原始密码
@property (nonatomic, strong) UILabel *pwdTitleLabelOne;  // 新密码
@property (nonatomic, strong) UILabel *pwdTitleLabelTwo; // 确认密码

// 分割线
@property (nonatomic, strong) UILabel *lineOne;
@property (nonatomic, strong) UILabel *lineTwo;

@end
@implementation XKUpdateLoginPwdView

#pragma mark - 重写构造方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configArguments];
        [self addSubviews];
    }
    return self;
}



- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField becomeFirstResponder];
}



#pragma mark - 判断按钮何时可以点击
- (void)textDidChange:(UITextField *)textField {
    if (self.pwdTextFieldZero.text.length >= 6 &&
        self.pwdTextFieldOne.text.length >= 6 &&
        self.pwdTextFieldTwo.text.length >= 6) {
        
        self.confirmBtn.enabled = YES;
    } else {
        self.confirmBtn.enabled = NO;
    }
    
}

#pragma mark - 控制字符输入长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    return text.length > 16 ? NO : YES;
}




#pragma mark - 设置自身UI
- (void)configArguments {
    self.backgroundColor = kBaseGrayColor;
}


#pragma mark - 创建子控件
- (void)addSubviews {
    self.bgView = [UIView new];
    self.bgView.backgroundColor = WhiteColor;
    
    self.lineOne = [UILabel new];
    self.lineOne.backgroundColor = kBaseGrayColor;
    self.lineTwo = [UILabel new];
    self.lineTwo.backgroundColor = kBaseGrayColor;
    
    
    self.remindLabel = [UILabel getLabWithText:@"小康100提示您:请牢记新密码; 不定期更换密码可以确保您的账户安全!" textFont:Font_13 textColor:RedColor textAlignment:NSTextAlignmentCenter numberOfLines:0];
    self.pwdTitleLabelZero = [UILabel getLabWithText:@"原  密  码" textFont:Font_15 textColor:BlackColor];
    self.pwdTitleLabelOne = [UILabel getLabWithText:@"新  密  码" textFont:Font_15 textColor:BlackColor];
    self.pwdTitleLabelTwo = [UILabel getLabWithText:@"确认密码" textFont:Font_15 textColor:BlackColor];
    
    self.pwdTextFieldZero = [UITextField new];
    self.pwdTextFieldZero.placeholder = @"请输入当前登录密码";
    self.pwdTextFieldZero.font = Font(15);
    self.pwdTextFieldZero.delegate = self;
    self.pwdTextFieldZero.secureTextEntry = YES;
    self.pwdTextFieldZero.borderStyle = UITextBorderStyleNone;
    self.pwdTextFieldZero.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.pwdTextFieldZero addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.pwdTextFieldOne = [UITextField new];
    self.pwdTextFieldOne.placeholder = @"请输入6-16位新登录密码";
    self.pwdTextFieldOne.font = Font(15);
    self.pwdTextFieldOne.delegate = self;
    self.pwdTextFieldOne.secureTextEntry = YES;
    self.pwdTextFieldOne.borderStyle = UITextBorderStyleNone;
    self.pwdTextFieldOne.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.pwdTextFieldOne addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.pwdTextFieldTwo = [UITextField new];
    self.pwdTextFieldTwo.placeholder = @"请确认新登录密码";
    self.pwdTextFieldTwo.font = Font(15);
    self.pwdTextFieldTwo.delegate = self;
    self.pwdTextFieldTwo.secureTextEntry = YES;
    self.pwdTextFieldTwo.borderStyle = UITextBorderStyleNone;
    self.pwdTextFieldTwo.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.pwdTextFieldTwo addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.confirmBtn = [UIButton getClickBtnWithTitle:@"确认修改"];

    
    [self addSubview:self.bgView];
    [self addSubview:self.remindLabel];
    
    [self addSubview:self.pwdTitleLabelZero];
    [self addSubview:self.pwdTitleLabelOne];
    [self addSubview:self.pwdTitleLabelTwo];
    
    [self addSubview:self.pwdTextFieldZero];
    [self addSubview:self.pwdTextFieldOne];
    [self addSubview:self.pwdTextFieldTwo];
    
    [self addSubview:self.lineOne];
    [self addSubview:self.lineTwo];
    
    [self addSubview:self.confirmBtn];
    
    // 在添加子控件之后再添加约束
    [self addConstraints];
}

#pragma mark - 添加约束
- (void)addConstraints {
    
    
    
    [self.remindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(20*Scale_H);
        make.left.mas_equalTo(self).offset(20*Scale_W);
        make.right.mas_equalTo(self).offset(-20*Scale_W);
    }];
    
    [self.pwdTitleLabelZero mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(30*Scale_W);
        make.top.mas_equalTo(self.remindLabel.mas_bottom).offset(30*Scale_H);
        make.width.mas_equalTo(80*Scale_W);
    }];
    
    [self.pwdTextFieldZero mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.pwdTitleLabelZero.mas_right).offset(10*Scale_W);
        make.centerY.mas_equalTo(self.pwdTitleLabelZero);
        make.right.mas_equalTo(self).offset(-30*Scale_W);
    }];
    
    [self.lineOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pwdTitleLabelZero.mas_bottom).offset(15*Scale_H);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(10*Scale_H);
    }];
    
    [self.pwdTitleLabelOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.pwdTitleLabelZero);
        make.top.mas_equalTo(self.lineOne.mas_bottom).offset(15*Scale_H);
    }];

    [self.pwdTextFieldOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.pwdTextFieldZero);
        make.centerY.mas_equalTo(self.pwdTitleLabelOne);
    }];
    
    [self.lineTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pwdTitleLabelOne.mas_bottom).offset(15*Scale_H);
        make.left.right.mas_equalTo(self.lineOne);
        make.height.mas_equalTo(1*Scale_H);
    }];
    
    [self.pwdTitleLabelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.pwdTitleLabelZero);
        make.top.mas_equalTo(self.lineTwo.mas_bottom).offset(15*Scale_H);
    }];
    
    [self.pwdTextFieldTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.pwdTextFieldZero);
        make.centerY.mas_equalTo(self.pwdTitleLabelTwo);
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(self.pwdTitleLabelZero).offset(-15*Scale_H);
        make.bottom.mas_equalTo(self.pwdTitleLabelTwo).offset(15*Scale_H);
    }];
    
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(30*Scale_W);
        make.right.mas_equalTo(self).offset(-30*Scale_W);
        make.height.mas_equalTo(44*Scale_H);
        make.top.mas_equalTo(self.bgView.mas_bottom).offset(20*Scale_H);
    }];
}

@end
